package com.tdam2011.grupo02.support;

import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.ListView;

public abstract class MyGestureDetector extends SimpleOnGestureListener {

	private static final int REL_SWIPE_MIN_DISTANCE = 120;
	private static final int REL_SWIPE_MAX_OFF_PATH = 250;
	private static final int REL_SWIPE_THRESHOLD_VELOCITY = 100;

	ListView listView;
	
	public MyGestureDetector(ListView lv){
		listView = lv;
	}
	
	// Detect a single-click and call my own handler.
	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		int pos = listView.pointToPosition((int) e.getX(), (int) e.getY());
		myOnItemClick(pos);
		return false;
	}

	public abstract void myOnItemClick(int pos);
	public abstract void onRTLFling(int pos);
	public abstract void onLTRFling(int pos);
	
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if (Math.abs(e1.getY() - e2.getY()) > REL_SWIPE_MAX_OFF_PATH)
			return false;
		if (e1.getX() - e2.getX() > REL_SWIPE_MIN_DISTANCE
				&& Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {
			int pos = listView.pointToPosition((int) e1.getX(), (int) e1.getY());
			onRTLFling(pos);
		} else if (e2.getX() - e1.getX() > REL_SWIPE_MIN_DISTANCE
				&& Math.abs(velocityX) > REL_SWIPE_THRESHOLD_VELOCITY) {
			int pos = listView.pointToPosition((int) e1.getX(), (int) e1.getY());
			onLTRFling(pos);
		}
		return false;
	}
}
